package org.ztr.yanai.blog.utils;

import org.ztr.yanai.blog.dto.PostDTO;
import org.ztr.yanai.blog.dto.UserDTO;
import org.ztr.yanai.blog.entity.Post;
import org.ztr.yanai.blog.entity.User;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 14:30
 * @Description: 实体类与DTO转换工具类
 * @Version: 1.0
 */
public class EntityDtoConverter {

    private EntityDtoConverter() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * User Entity 转 UserDTO
     */
    public static UserDTO toUserDTO(User user) {
        if (user == null) {
            return null;
        }
        UserDTO dto = new UserDTO();
        dto.setId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setEmail(user.getEmail());
        dto.setAvatar(user.getAvatar());
        dto.setRole(user.getRole());
        dto.setStatus(user.getStatus());
        dto.setCreatedAt(user.getCreatedAt());
        dto.setLastLogin(user.getLastLogin());
        return dto;
    }

    /**
     * Post Entity 转 PostDTO（不含作者信息）
     */
    public static PostDTO toPostDTO(Post post) {
        if (post == null) {
            return null;
        }
        PostDTO dto = new PostDTO();
        dto.setId(post.getId());
        dto.setTitle(post.getTitle());
        dto.setContent(post.getContent());
        dto.setUserId(post.getUserId());
        dto.setStatus(post.getStatus());
        dto.setViews(post.getViews());
        dto.setLikes(post.getLikes());
        dto.setCreatedAt(post.getCreatedAt());
        dto.setUpdatedAt(post.getUpdatedAt());
        return dto;
    }

    /**
     * Post Entity 转 PostDTO（含作者信息）
     */
    public static PostDTO toPostDTO(Post post, User author) {
        PostDTO dto = toPostDTO(post);
        if (dto != null && author != null) {
            dto.setAuthorName(author.getUsername());
            dto.setAuthorAvatar(author.getAvatar());
        }
        return dto;
    }
}
